#include <avr/io.h>

#define B19200 51
#define B38400 25

void UART_init()
{
//	DDRD=DDRD | (1<<PD1);			// PORT D TXD out
	DDRD=DDRD & ~(1<<PD0);			// PORT D RXD in
	PORTD=PORTD | (1<<PD0);			// PUSH-PULL
	UBRRH=0;						//	Baud
	UBRRL=B19200;					//  19200
	UCSRB=(1<<RXCIE)+(1<<RXEN);		// Only RX enable, RX int enable
	UCSRC=(1<<UCSZ1)+(1<<UCSZ0);	// 8-bit 
}

void send(unsigned char byte)
{
	while (!(UCSRA & (1 << UDRE))) {}
	UDR=byte;
}

int receive()
{
	uint16_t timeout=40000;

	while (!(UCSRA & (1 << RXC)) && timeout>0) { timeout--; }
	return UDR;
}
